
int SensorPin = A0;
int door = 5;
int OneBtn = 7;
int TwoBtn = 6;
int ThreeBtn = 9;
int FourBtn = 8;
String password = "0";
int OneState;
int lastOneBtnState = LOW;
int TwoState;
int lastTwoBtnState = LOW;
int ThreeState;
int lastThreeBtnState = LOW;
int FourState;
int lastFourBtnState = LOW;

long lastDebounceTime = 0;
long debounceDelay = 50;

int PlayCount = 0;

//播放
unsigned char playWarning[8] = {0x04, 0xFB, 0x03, 0x06, 0x00, 0x02, 0x0A};
//停止
unsigned char pause[8] = {0x04, 0xFB, 0x01, 0x03, 0x03, 0x06};
//循环模式
unsigned char cycle[8] = {0x0B, 0xF4, 0x02, 0x01, 0x00, 0x02};



void setup() {
  Serial.begin(9600, SERIAL_8N1);
  pinMode(SensorPin, INPUT);
  pinMode(OneBtn, INPUT);
  pinMode(TwoBtn, INPUT);
  pinMode(ThreeBtn, INPUT);
  pinMode(FourBtn, INPUT);
  pinMode(door, OUTPUT);
  Serial.write(pause, 8);

}

void openDoor(){
  digitalWrite(door,HIGH);
  delay(2000);
  digitalWrite(door,LOW);
}
void loop() {

  int OneBtnState = digitalRead(OneBtn);
  int TwoBtnState = digitalRead(TwoBtn);
  int ThreeBtnState = digitalRead(ThreeBtn);
  int FourBtnState = digitalRead(FourBtn);
  //1
  if (OneBtnState != lastOneBtnState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (OneBtnState != OneState) {
      OneState = OneBtnState;
      if (OneBtnState == HIGH) {
        password.concat("1");
        Serial.println(1);
      }
    }
  }
  lastOneBtnState = OneBtnState;

  //2
  if (TwoBtnState != lastTwoBtnState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (TwoBtnState != TwoState) {
      TwoState = TwoBtnState;
      if (TwoBtnState == HIGH) {
        password.concat("2");
        Serial.println(2);
      }
    }
  }
  lastTwoBtnState = TwoBtnState;

  //3
  if (ThreeBtnState != lastThreeBtnState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (ThreeBtnState != ThreeState) {
      ThreeState = ThreeBtnState;
      if (ThreeBtnState == HIGH) {
        password.concat("3");
        Serial.println(3);
      }
    }
  }
  lastThreeBtnState = ThreeBtnState;

  //4
  if (FourBtnState != lastFourBtnState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (FourBtnState != FourState) {
      FourState = FourBtnState;
      if (FourBtnState == HIGH) {
        password.concat("4");
        Serial.println(4);
      }
    }
  }
  lastFourBtnState = FourBtnState;

  //自动清除输入密码字符
  if ((millis() - lastDebounceTime) == 10000 && password != "0") {
    password = "0";
    Serial.println("超出10秒,自动清除");
  }

  if (password.length() == 6) {
    password = "0";
    Serial.println("密码清空");
  }

  //输入密码关闭播放
  if (password == "04412") {
    Serial.println(password);
    Serial.write(pause, 8);
    openDoor();
    password = "0";
    return;
  } else {
    //人体感应播放音乐
//    int val = analogRead(SensorPin);
//
//    if (val > 0) {
//        Serial.write(playWarning, 8);
//    }
  }

}
